//
//  COCOAFANBubbleView.m
//  CocoaFan
//
//  Created by Chen Xianan on 2/28/09.
//  Copyright 2009 realazy.org All rights reserved.
//

#import "COCOAFANBubbleView.h"
#import "COCOAFANTableView.h"

@implementation COCOAFANBubbleView

@synthesize statusType, selected, read;

- (void)drawRect:(NSRect)rect {
	NSRect bounds = [self bounds];
	NSRect bpRect = NSMakeRect(bounds.origin.x+3, bounds.origin.y+8, bounds.size.width-8, bounds.size.height-10);
	NSBezierPath *bp = [self bezierPathWithRect:bpRect];

	if (![self isSelected]){
		[NSGraphicsContext saveGraphicsState]; 
		NSShadow* theShadow = [[NSShadow alloc] init];
		[theShadow setShadowOffset:NSMakeSize(0.0, -1.0)];
		[theShadow setShadowBlurRadius:2.0];
		[theShadow setShadowColor:[[NSColor blackColor] colorWithAlphaComponent:0.3]];
		[theShadow set]; 
		[[NSColor colorWithDeviceRed:0.69 green:0.77 blue:0.70 alpha:1.00] set];
		[bp setLineWidth:1.0];
		[bp stroke];
		[NSGraphicsContext restoreGraphicsState];
		[theShadow release];
	} else {
		NSResponder *resp = [[self window] firstResponder];
		COCOAFANTableView *table = (COCOAFANTableView *)[[self superview] superview];
		NSText *editorObject = (NSText *)[[self window] fieldEditor:NO
																  forObject:self];
		if (![NSApp isActive] || (resp != table && editorObject != [statusField currentEditor]))
			[[NSColor darkGrayColor] set];
		else
			[[NSColor colorWithCalibratedRed:0.22 green:0.46 blue:0.84 alpha:1.00] set];
		
		[bp setLineWidth:4.0];
		[bp stroke];
	}

	NSColor *startingColor;
	NSColor *endingColor;
	switch([self statusType]){
	case COCOAFAN_STATUS_OWN:
		startingColor = [NSColor colorWithCalibratedRed:0.91 green:0.92 blue:0.86 alpha:1.00];
		endingColor = [NSColor colorWithCalibratedRed:0.82 green:0.84 blue:0.77 alpha:1.00];
		break;
	case COCOAFAN_STATUS_PMSG:
		startingColor = [NSColor colorWithCalibratedRed:0.82 green:0.82 blue:0.89 alpha:1.00];
		endingColor = [NSColor colorWithCalibratedRed:0.76 green:0.76 blue:0.86 alpha:1.00];
		break;
	case COCOAFAN_STATUS_REPLY:
		startingColor = [NSColor colorWithCalibratedRed:0.81 green:0.87 blue:0.89 alpha:1.00];
		endingColor = [NSColor colorWithCalibratedRed:0.72 green:0.78 blue:0.80 alpha:1.00];
		break;
	case COCOAFAN_STATUS_NORMAL:
	default:
		startingColor = [NSColor colorWithCalibratedRed:0.99 green:0.99 blue:0.99 alpha:1.00];
		endingColor = [NSColor colorWithCalibratedRed:0.87 green:0.87 blue:0.87 alpha:1.00];
		break;
	}
		
	NSGradient *grd = [[NSGradient alloc] initWithStartingColor:startingColor
													endingColor:endingColor];
	[grd drawInBezierPath:bp angle:-90];
	[grd release];
}

- (NSBezierPath *)bezierPathWithRect:(NSRect)rect
{
	NSBezierPath *bp = [NSBezierPath bezierPath];
	float leftOX = rect.origin.x; 
	float leftX = leftOX + ARROW_WIDTH;
	float rightX = leftOX + rect.size.width;
	float bottomY = rect.origin.y;
	float topY = bottomY + rect.size.height;
	
	NSPoint startingPoint = NSMakePoint(leftX + ARROW_WIDTH, topY);
	[bp moveToPoint:startingPoint];
	NSPoint cp;
	// top right corner
	[bp lineToPoint:NSMakePoint(rightX-CORNER_RADIUS, topY)];
	cp = NSMakePoint(rightX, topY);
	[bp curveToPoint:NSMakePoint(rightX,  topY - CORNER_RADIUS)
	   controlPoint1:cp
	   controlPoint2:cp];
	// bottom right corner
	[bp lineToPoint:NSMakePoint(rightX, bottomY + CORNER_RADIUS)];
	cp = NSMakePoint(rightX, bottomY);
	[bp curveToPoint:NSMakePoint(rightX - CORNER_RADIUS, bottomY)
	   controlPoint1:cp
	   controlPoint2:cp];
	// bottom left corner
	[bp lineToPoint:NSMakePoint(leftX + CORNER_RADIUS, bottomY)];
	cp = NSMakePoint(leftX, bottomY);
	[bp curveToPoint:NSMakePoint(leftX, bottomY + CORNER_RADIUS)
	   controlPoint1:cp
	   controlPoint2:cp];
	// arrow
	[bp lineToPoint:NSMakePoint(leftX, topY-ARROW_HEIGHT-ARROW_TOP)];
	[bp lineToPoint:NSMakePoint(leftOX, topY-ARROW_HEIGHT/2-ARROW_TOP)];
	[bp lineToPoint:NSMakePoint(leftX, topY-ARROW_TOP)];
	// top left corner
	[bp lineToPoint:NSMakePoint(leftX, topY - CORNER_RADIUS)];
	cp = NSMakePoint(leftX, topY);
	[bp curveToPoint:startingPoint
	   controlPoint1:cp
	   controlPoint2:cp];

	[bp closePath];

	[bp setLineJoinStyle:NSMiterLineJoinStyle];
	return bp;
}

@end
